__author__ = 'shawhen'

from ..platform import what as m_what_platform


class UMutex():
    def __init__(self, name=None):
        self._handle = None

        if m_what_platform.running_os["type"] == "windows":
            from ..sysapi.win32.um import synchapi as m_synchapi

            self._handle = m_synchapi.CreateMutex(None, False, name)
        else:
            raise NotImplementedError

    def acquire(self, timeout=-1):
        if m_what_platform.running_os["type"] == "windows":
            from ..sysapi.win32.um import synchapi as m_synchapi
            from ..sysapi.win32.um import WinBase as m_um_WinBase

            if timeout == -1:
                timeout = m_um_WinBase.INFINITE
            return m_synchapi.WaitForSingleObject(self._handle, timeout)
        else:
            raise NotImplementedError

    def release(self):
        if m_what_platform.running_os["type"] == "windows":
            from ..sysapi.win32.um import synchapi as m_synchapi

            return m_synchapi.ReleaseMutex(self._handle)
        else:
            raise NotImplementedError